<?php

namespace App\Console\Commands\WorkOrder;

use App\Models\WorkOrder\WorkOrderApplyManagerModel;
use App\Models\WorkOrder\WorkOrderUserRoleModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FixApplyManagerCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workOrder:fixApplyManager';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '工单：历史数据修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        WorkOrderApplyManagerModel::query()->chunkById(100, function (Collection $collection) {
            $collection->each(function (WorkOrderApplyManagerModel $applyManagerModel) {
                if ($applyManagerModel->role == WorkOrderUserRoleModel::ROLE_TYPE_1) {
                    if ($applyManagerModel->remark) {
                        $applyManagerModel->update(["submitTime" => $applyManagerModel->updatedAt->timestamp]);
                    }
                } else {
                    if ($applyManagerModel->solution) {
                        $applyManagerModel->update(["submitTime" => $applyManagerModel->updatedAt->timestamp]);
                    }
                }
            });
        });
        return 0;
    }
}
